<template>
<el-container>
   
<div class="formCss">
   <span class="timeCss">{{message}}</span>
<el-form ref="schedule" :model="schedule" label-width="80px">
  <el-form-item label="日程名称" class="eventCss">
    <el-input ref="event"  v-model="schedule.event"></el-input><span class="errCss">{{error.event}}</span>
  </el-form-item>
   <el-form-item label="日程时间" class="labCss">
    <el-col :span="11"  class="dateCss">
      <el-date-picker ref="startTime" value-format="YYYY-MM-DD"  type="date" v-model="schedule.startTime" style="width: 100%;"></el-date-picker><span class="errCss">{{error.startTime}}</span>
    </el-col>
    <el-col :span="2" class="line">-</el-col>
    <el-col :span="11"  class="dateCss">
      <el-date-picker ref="endTime" value-format="YYYY-MM-DD"  type="date"  v-model="schedule.endTime" style="width: 100%;"></el-date-picker><span class="errCss">{{error.endTime}}</span>
    </el-col>
  </el-form-item>
  <el-form-item>
      <div class="butCss">
        <el-button align="center" type="primary" @click="onSubmit">保存</el-button>
        <el-button align="center" type="primary" @click="cancel">取消</el-button>
     </div>
          
  </el-form-item>
</el-form>
</div>
</el-container>
</template>

<script>
import PersonalMenu from '@/components/menu/PersonalMenu.vue'
import instance from '@/utlis/request.js'

export default {
    name: 'AddView',
    components: {
        PersonalMenu
    },
    data() {
        
        return {
           schedule: {
          event:"",
          startTime:"",
          endTime:""
        },
        message:"",
        error:{
               event:"",
               startTime:"",
               endTime:""  
          }
        };
    },
    created:function(){
          this.initData();
    },
    methods: {
         initData(){
            //获得列表页面传过来的参数
            var rowId = this.$route.params.id;
            instance.get("Web/schedule/findById",{params:{id:rowId}}).then(res=>{
                this.schedule=res.data.data;
            });
        },
       //修改日程的功能
      onSubmit() {
        
          //日程名称格式判断
          if(this.schedule.event==undefined||this.schedule.event.trim()==""){
            this.error.event="日程名称不能为空";
            this.$refs.event.select();
            return;
          }else{
              this.error.event="";
          }
          //开始时间格式判断
          if(this.schedule.startTime==undefined||this.schedule.startTime==""){
              this.error.startTime="开始时间不能为空";
              this.$refs.startTime.focus();
              return;
          }else{
              this.error.startTime="";
          }
          //结束时间格式判断
          if(this.schedule.endTime==undefined||this.schedule.endTime==""){
              this.error.endTime="结束时间不能为空";
              this.$refs.endTime.focus();
              return;
          }else{
              this.error.endTime="";
          }
            //判断日程时间(结束时间不能小于开始时间)
           let date1 = new Date(this.schedule.startTime).getTime()
           //1646697600000
           console.log(date1);
           let date2 = new Date(this.schedule.endTime).getTime()
           //1647216000000
           console.log(date2);
           if(date1>date2){
              this.message="结束时间不能小于开始时间";
              return;
           }else{
             this.message=""
           }
          instance.post("Web/schedule/updateScheule",this.schedule).then(res=>{
             this.$router.push({path:"/PersonalCalendarView"});
          })
         
      },
      //取消功能
      cancel(){
        this.$router.push({path:"/PersonalCalendarView"});
      }
    }

}
</script>
<style scoped>
    .eventCss{
        margin-top: 100px;
        width: 680px;   
    }
    .dateCss{
        width: 320px;
    }
    .labCss{
       margin-top: 30px; 
    }
    .formCss{
         margin-left: 300px;
    }
    .butCss{
        margin-left: 260px;
        margin-top: 20px;
    }
    .errCss{
        color:red;
    }
   .timeCss{
      color:red;
   }

</style>